package shape;


import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfWriter;
import constant.Constant;
import entry.Position;

import java.util.HashMap;
import java.util.Map;


public class JiaoZhuangShape implements Shape {
    private Position position;
    private int pageCount;
    private int rotate;
    private float w;
    private float h;
    private int mIndex;
    private float cx;
    private float jj;
    private float space;
    private float jx;
    private int index;
    private float v;
    private int pos;


    public JiaoZhuangShape(Position position, int pageCount, int rotate, float w, float h, int mIndex, float cx, float space, float jj, float jx, int index, float v, int pos) {
        this.position = position;
        this.pageCount = pageCount;
        this.rotate = rotate;
        this.w = w;
        this.h = h;
        this.mIndex = mIndex;
        this.cx = cx;
        this.space = space;
        this.jj = jj;
        this.jx = jx;
        this.index = index;
        this.v = v;
        this.pos = pos;

    }


    @Override
    public void draw(PdfContentByte cb) {

    }

    @Override
    public void draw(PdfContentByte cb, PdfWriter writer) {
        try {
//            importedPage.setBoundingBox(new Rectangle(mask*Constant.pt,importedPage.getHeight()-mask*Constant.pt,importedPage.getWidth()    -mask*Constant.pt,mask*Constant.pt));
            if (this.rotate == 0) {
                PdfImportedPage importedPage = writer.getImportedPage(Constant.pdfReaderMap.get(this.mIndex), pageCount);
                importedPage.setBoundingBox(getRectangle(this.mIndex));
                cb.addTemplate(importedPage, (position.getX() - cx + jj + jx) * Constant.pt, (position.getY() - cx + jj + jx) * Constant.pt);
            } else if (this.rotate == 90) {
                PdfImportedPage importedPage = writer.getImportedPage(Constant.pdfReaderMap.get(this.mIndex), pageCount);
                cb.addTemplate(importedPage, 0, 1, -1, 0, position.getX() * Constant.pt + importedPage.getHeight(), position.getY() * Constant.pt);
//                importedPage.setBoundingBox(new Rectangle(0,0,importedPage.getHeight()+mask*pt/2,importedPage.getWidth()+mask*pt/2));
            } else if (this.rotate == 270) {
                PdfImportedPage importedPage = writer.getImportedPage(Constant.pdfReaderMap.get(this.mIndex), pageCount);
                cb.addTemplate(importedPage, 0, -1, 1, 0, position.getX() * Constant.pt, position.getY() * Constant.pt + importedPage.getWidth());
//                importedPage.setBoundingBox(new Rectangle(mask*pt,mask*pt,importedPage.getHeight(),importedPage.getWidth()));
            } else if (this.rotate == 180) {
                PdfImportedPage importedPage = writer.getImportedPage(Constant.pdfReaderMap.get(this.mIndex), pageCount);
                importedPage.setBoundingBox(getRectangle(this.mIndex));
                cb.addTemplate(importedPage, -1, 0, 0, -1, (position.getX() + jj + jx + cx + w) * Constant.pt, (position.getY() + jj + jx + cx + h) * Constant.pt);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Rectangle getRectangle(int mIndex) {
        float pt = 72f / 25.4f;
        float c = cx * pt;
        float s = space * pt;
        float j = jj * pt;
        float v = this.v * pt;
        if (this.index != 1) {

            Rectangle rectangle = new Rectangle(0, 0, w * pt + c + s / 2, h * pt + c + s / 2);
            if (this.rotate == 0) {
                switch (mIndex) {
                    case 1:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + s / 2);

                        break;
                    case 4:

                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + s / 2, h * pt + c + s / 2);


                        break; //可选
                    case 7:

                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + s / 2);


                        break; //可选
                    case 2:

                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + s / 2, h * pt + c + s / 2);


                        break;
                    case 5:

                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + s / 2);


                        break; //可选
                    case 8:

                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + j, h * pt + c + s / 2);


                        break; //可选
                    case 3:


                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + s / 2, h * pt + c + j);

                        break;
                    case 6:

                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + j);

                        break; //可选
                    case 9:

                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + j, h * pt + c + j);

                        break; //可选
                    case 10:
                        if(pos==1){
                            rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + j);
                        }else if(pos==4){
                            rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + j);
                        }else{
                            rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + j);
                        }

                        break; //可选
                    case 11:
                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + s / 2, h * pt + c + j);
                        break; //可选
                    case 12:
                        if(pos==1){
                            rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + j);
                        }else if(pos==4){
                            rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + j);
                        }else{
                            rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + j);
                        }

                        break; //可选
                    case 13:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 14:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 15:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 16:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + j);
                        break; //可选
                }
            } else if (this.rotate == 180) {
                Map<Integer, Integer> map = new HashMap<Integer, Integer>();
                map.put(1, 9);
                map.put(9, 1);
                map.put(2, 8);
                map.put(8, 2);
                map.put(3, 7);
                map.put(7, 3);
                map.put(4, 6);
                map.put(6, 4);
                map.put(5, 5);
                map.put(16, 16);
                map.put(14, 14);
                map.put(11, 11);
                map.put(10, 12);
                map.put(12, 10);
                map.put(13, 15);
                map.put(15, 13);

                switch (map.get(mIndex)) {
                    case 1:

                        rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + s / 2);

                        break;
                    case 4:

                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + s / 2, h * pt + c + s / 2);

                        break; //可选
                    case 7:

                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + s / 2);


                        break; //可选
                    case 2:

                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + s / 2, h * pt + c + s / 2);

                        break;
                    case 5:

                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + s / 2);

                        break; //可选
                    case 8:

                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + j, h * pt + c + s / 2);

                        break; //可选
                    case 3:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + s / 2, h * pt + c + j);

                        break;
                    case 6:
                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + j);
                        break; //可选
                    case 9:
                        rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 10:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + s / 2, h * pt + c + j);
                        break; //可选
                    case 11:
                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + s / 2, h * pt + c + j);
                        break; //可选
                    case 12:
                        rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 13:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 14:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 15:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 16:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + j);
                        break; //可选
                }
            }
            return rectangle;
        } else {
            Rectangle rectangle = new Rectangle(0, 0, w * pt + c + s / 2, h * pt + c + s / 2);
            if (this.rotate == 0) {
                switch (mIndex) {
                    case 1:

                        rectangle = new Rectangle(c - j, c - j, w * pt + c + v / 2, h * pt + c + s / 2);

                        break;
                    case 4:
                        if(pos == 2){
                            rectangle = new Rectangle(c -v / 2, c - j, w * pt + c +s/ 2, h * pt + c + s / 2);

                        }else if(pos==3){
                            rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + v / 2, h * pt + c + s / 2);
                        }


                        break; //可选
                    case 7:

                        rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + j, h * pt + c + s / 2);


                        break; //可选
                    case 2:

                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + v / 2, h * pt + c + s / 2);


                        break;
                    case 5:
                        if(pos==2){
                            rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + s / 2);
                        }else if(pos==3){
                            rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + v / 2, h * pt + c + s / 2);
                        }




                        break; //可选
                    case 8:

                        rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + j, h * pt + c + s / 2);


                        break; //可选
                    case 3:


                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + v / 2, h * pt + c + j);

                        break;
                    case 6:
                        if (pos == 2) {
                            rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + j);
                        } else if (pos == 3) {
                            rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + v / 2, h * pt + c + j);
                        }


                        break; //可选
                    case 9:

                        rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + j, h * pt + c + j);

                        break; //可选
                    case 10:
                        if(pos==4){
                            rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + j, h * pt + c + j);
                        }else if(pos==1){
                            rectangle = new Rectangle(c - j, c - j, w * pt + c + v / 2, h * pt + c + j);
                        }else{
                            rectangle = new Rectangle(c - j, c - j, w * pt + c + v / 2, h * pt + c + j);
                        }

                        break; //可选
                    case 11:
                        rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + v / 2, h * pt + c + j);
                        break; //可选
                    case 12:
                        if(pos==4){
                            rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + j, h * pt + c + j);
                        }else if(pos==1){
                            rectangle = new Rectangle(c - j, c - j, w * pt + c + v / 2, h * pt + c + j);
                        }else{
                            rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + j, h * pt + c + j);
                        }

                        break; //可选
                    case 13:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 14:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 15:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 16:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + j);
                        break; //可选
                }
            } else if (this.rotate == 180) {
                Map<Integer, Integer> map = new HashMap<Integer, Integer>();
                map.put(1, 9);
                map.put(9, 1);
                map.put(2, 8);
                map.put(8, 2);
                map.put(3, 7);
                map.put(7, 3);
                map.put(4, 6);
                map.put(6, 4);
                map.put(5, 5);
                map.put(16, 16);
                map.put(14, 14);
                map.put(11, 11);
                map.put(10, 12);
                map.put(12, 10);
                map.put(13, 15);
                map.put(15, 13);

                switch (map.get(mIndex)) {
                    case 1:

                        rectangle = new Rectangle(c - j, c - j, w * pt + c + v / 2, h * pt + c + s / 2);

                        break;
                    case 4:
                        if(this.pos==2){
                            rectangle = new Rectangle(c - s / 2, c - j, w * pt + c + v/ 2, h * pt + c + s / 2);
                        }else{
                            rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + s / 2, h * pt + c + s / 2);
                        }



                        break; //可选
                    case 7:

                        rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + j, h * pt + c + s / 2);


                        break; //可选
                    case 2:

                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + v / 2, h * pt + c + s / 2);

                        break;
                    case 5:
                        if(this.pos==2){
                            rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + v / 2, h * pt + c + s / 2);

                        }else{
                            rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + s / 2);

                        }

                        break; //可选
                    case 8:

                        rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + j, h * pt + c + s / 2);

                        break; //可选
                    case 3:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + v / 2, h * pt + c + j);

                        break;
                    case 6:
                        if(this.pos==2){
                            rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + s / 2, h * pt + c + j);
                        }else{
                            rectangle = new Rectangle(c - s / 2, c - s / 2, w * pt + c + v / 2, h * pt + c + j);
                        }

                        break; //可选
                    case 9:
                        rectangle = new Rectangle(c - v / 2, c - s / 2, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 10:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + v / 2, h * pt + c + j);
                        break; //可选
                    case 11:
                        rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + v / 2, h * pt + c + j);
                        break; //可选
                    case 12:
                        rectangle = new Rectangle(c - v / 2, c - j, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 13:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + j);
                        break; //可选
                    case 14:
                        rectangle = new Rectangle(c - j, c - s / 2, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 15:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + s / 2);
                        break; //可选
                    case 16:
                        rectangle = new Rectangle(c - j, c - j, w * pt + c + j, h * pt + c + j);
                        break; //可选
                }
            }
            return rectangle;
        }


    }


}
